﻿using PlayListSet.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PlayListSet.UI
{
    /// <summary>
    /// AppConfigSet.xaml 的交互逻辑
    /// </summary>
    public partial class AppConfigSet : Window
    {
        private AudioInfo ltcAudioInfoSelect;
        private AudioInfo localAudioInfoSelect;

        public AppConfig AppConfig { get; set; }
        public List<Entity.AudioInfo> AudioInfos { get; set; }
        public AudioInfo LTCAudioInfoSelect
        {
            set
            {
                ltcAudioInfoSelect = value;
                AppConfig.LTCAudioDevice = value.ShowStr;
            }
            get
            {
                return ltcAudioInfoSelect;
            }
        }
        public AudioInfo LocalAudioInfoSelect
        {
            set
            {
                localAudioInfoSelect = value;
                AppConfig.LocalAudioDevice = value.ShowStr;
            }
            get
            {
                return localAudioInfoSelect;
            }
        }
        public AppConfigSet()
        {
            InitializeComponent();
            AppConfig = AppConfig.Read();
            AudioInfos = AudioInfo.GetAudioInfos();
            AudioInfos.Insert(0, AudioInfo.GetAudio(""));
            var a = AudioInfo.GetAudio(AppConfig.LTCAudioDevice);
            for (int i = 0; i < AudioInfos.Count; i++)
            {
                if (AudioInfos[i].ManufacturerGuid == a.ManufacturerGuid && AudioInfos[i].ProductName == a.ProductName)
                {
                    ltcAudioInfoSelect = AudioInfos[i];
                    break;
                }
            }


            var b = AudioInfo.GetAudio(AppConfig.LocalAudioDevice);
            for (int i = 0; i < AudioInfos.Count; i++)
            {
                if (AudioInfos[i].ManufacturerGuid==b.ManufacturerGuid&& AudioInfos[i].ProductName == b.ProductName)
                {
                    localAudioInfoSelect = AudioInfos[i];
                    break;
                }
            }
           

            this.DataContext = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "音频|*.mp3;*.wav;";
            if (openFileDialog.ShowDialog() == true)
            {
                AppConfig.LTCMp3 = openFileDialog.FileName;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                AppConfig.Save(AppConfig);
                MessageBox.Show("保存成功,重启生效");
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败："+ex.Message);
            }
        }
    }
}
